const Store = require('electron-store');
const store = new Store();

/**
 * 判断是否可以使用localStorage
 * @returns {boolean}
 */
export function islocalStorage() {
  return !!window.localStorage
}

/**
 * 获取某个localStorage
 * @param key
 * @returns {string|any}
 */
export function getStore(key) {
  return store.get(key)
}

/**
 * 设置某个localStorage
 * @param key
 * @param value
 */
export function setStore(key, value) {
  store.set(key, value);
}

/**
 * 删除某个localStorage
 * @param key
 */
export function delStore(key) {
  store.delete(key);
}

/**
 * 序列化
 * @param val
 * @returns {string}
 */
export function stringify(val){
  return val === undefined || typeof val === "function" ? val + "" : JSON.stringify(val);
}

/**
 * 反序列化
 * @param value
 * @returns {string|undefined|any|undefined}
 */
export function deserialize(value) {
  if (typeof value !== 'string') {
    return undefined
  }
  try {
    return JSON.parse(value)
  } catch (e) {
    return value || undefined
  }
}

/**
 * 是否JSON
 * @param obj
 * @returns {boolean}
 */
export function isJSON(obj) {
  return typeof obj === 'object' && Object.prototype.toString.call(obj).toLowerCase() === '[object object]' && !obj.length
}
